﻿
////////////////////////////////////////////////////////////////////////////////////////////
// 文件名: ETLTaskDAL.gen.cs
//
// 模块名称: ETLTaskDal
//
// 作者: yqf
//
// 历史记录（记录修改记录，修改文件后请添加修改记录，注明修改时间、修改人，修改内容）:
// 13-2-19    LPC    创建文件
////////////////////////////////////////////////////////////////////////////////////////////

using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using Common;
using Model;

namespace DAL
{
    /// <summary>
    /// SysUser表数据访问层
    /// </summary>
    public class Sys_LogDAL : DB
    { 
        public Sys_LogDAL(LoginInfo info)
        {
            base.loginInfo = info;
        }

        //创建数据库对象 
        /// <summary>
        /// 根据ID字段获取Sys_Log实体对象
        /// </summary> 
        public Sys_Log GetInfoById(string ID)
        {
            //添加输入输出参数
            SqlParameter[] sqlparams = { 
            new SqlParameter("@ID",ID )
          };

            DataTable dt = new DataTable();
            HIDB.dbo con = base.conn();
            //执行命令返回DataTable对象
            dt = con.StoredDataTable("Working_db.UP_Sys_Log_Item", sqlparams);

            if (dt != null && dt.Rows.Count > 0)
            {
                Sys_Log model = new Sys_Log
                {
                    GUID = dt.Rows[0]["GUID"].ToString(),
                    ParentID = dt.Rows[0]["ParentID"].ToString(),
                    OperationIP = dt.Rows[0]["OperationIP"].ToString(),
                    OperationAction = dt.Rows[0]["OperationAction"].ToString(),
                    OperationStatus = dt.Rows[0]["OperationStatus"].ToString() == "1" ? "成功" : "失败",
                    OperationResult = dt.Rows[0]["OperationResult"].ToString(),
                    OperationUser = dt.Rows[0]["OperationUser"].ToString(),
                    OperationTime = Convert.ToDateTime(dt.Rows[0]["OperationTime"]).ToString("yyyy-MM-dd HH:mm:ss")
                };
                return model;
            }

            return null;
        }
        public Sys_Log GetLogInfo(string user)
        {
            //添加输入输出参数
            SqlParameter[] sqlparams = { 
            new SqlParameter("@OperationUser",user )
          };

            DataTable dt = new DataTable();
            HIDB.dbo con = HIDB.common.make();
            dt = con.StoredDataTable("Working_db.UP_Sys_Log_GetLogInfo", sqlparams);

            if (dt != null && dt.Rows.Count > 0)
            {
                int di = dt.Rows.Count - 1;
                Sys_Log model = new Sys_Log
                {
                    GUID = dt.Rows[di]["GUID"].ToString(),
                    ParentID = dt.Rows[di]["ParentID"].ToString(),
                    OperationIP = dt.Rows[di]["OperationIP"].ToString(),
                    OperationAction = dt.Rows[di]["OperationAction"].ToString(),
                    OperationStatus = dt.Rows[di]["OperationStatus"].ToString() == "1" ? "成功" : "失败",
                    OperationResult = dt.Rows[di]["OperationResult"].ToString(),
                    OperationUser = dt.Rows[di]["OperationUser"].ToString(),
                    OperationTime = Convert.ToDateTime(dt.Rows[0]["OperationTime"]).ToString("yyyy-MM-dd HH:mm:ss")
                };
                return model;
            }

            return new Sys_Log();
        }
        /// <summary>
        /// 获取Sys_Log对象列表
        /// </summary>
        public  List<Sys_Log> GetList(string sql,IDataParameter[] sqlparams = null)
        {
            List<Sys_Log> list = new List<Sys_Log>();
            DataTable dt = new DataTable();
            HIDB.dbo con = base.conn();
            dt = con.GetDataTable(sql, sqlparams);

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    //string resultMsg = row["OperationResult"].ToString();
                    //if (resultMsg.Length > 37)
                    //    resultMsg = resultMsg.Substring(0, 37) + "...";
                    Sys_Log model = new Sys_Log()
                    {
                        GUID = row["GUID"].ToString(),
                        ParentID = row["ParentID"].ToString(),
                        OperationIP = row["OperationIP"].ToString(),
                        OperationAction = row["OperationAction"].ToString(),
                        OperationStatus = row["OperationStatus"].ToString(),
                        OperationResult = row["OperationResult"].ToString().Replace("\\r\\n",""),
                        OperationUser = row["OperationUser"].ToString(),
                        OperationTime = Convert.ToDateTime(row["OperationTime"]).ToString("yyyy-MM-dd HH:mm:ss")
                    };
                    list.Add(model);
                }
            }

            return list;
        }
        public int GetListCount(string sql, IDataParameter[] sqlparams)
        {
            DataTable dt = new DataTable();
            HIDB.dbo con = base.conn();
            dt = con.GetDataTable(sql, sqlparams);

            if (dt != null && dt.Rows.Count > 0)
            {
                return Int32.Parse(dt.Rows[0][0].ToString());
            }
            else
                return 0;
        }
        public bool ClearLog(string opUser, string date)
        {
            try
            {
                HIDB.dbo con = base.conn();
                IDataParameter[] sqlparams = new SqlParameter[1];
                sqlparams[0] = new SqlParameter("@DeletedDate", date);
                con.StoredUpdate("Working_db.UP_Sys_Log_ClearLog", sqlparams);
                return true;
            }
            catch (Exception ex) { throw ex; }
        }

        /// <summary>
        /// 根据ID字段获取Sys_Log实体对象
        /// </summary> 
        public Sys_DQLog GetDQInfoById(string ID)
        {
            //添加输入输出参数
            SqlParameter[] sqlparams = { 
            new SqlParameter("@ID",ID )
          };

            DataTable dt = new DataTable();
            HIDB.dbo con = base.conn();
            //执行命令返回DataTable对象
            dt = con.StoredDataTable("Working_db.UP_Sys_DQLog_Item", sqlparams);

            if (dt != null && dt.Rows.Count > 0)
            {
                Sys_DQLog model = new Sys_DQLog
                {
                    Guid = dt.Rows[0]["GUID"].ToString(),
                    ParentGUID = dt.Rows[0]["ParentGUID"].ToString(),
                    DQID = dt.Rows[0]["DQID"].ToString(),
                    DQName = dt.Rows[0]["DQName"].ToString(),
                    DQMessage = dt.Rows[0]["DQMessage"].ToString(),
                    DQTypeID = dt.Rows[0]["DQTypeID"].ToString(),
                    DQTime = Convert.ToDateTime(dt.Rows[0]["DQTime"]).ToString("yyyy-MM-dd HH:mm:ss")
                };
                return model;
            }

            return null;
        }

        /// <summary>
        /// 获取Sys_Log对象列表
        /// </summary>
        public List<Sys_DQLog> GetDQList(string sql ,IDataParameter[] sqlparams = null)
        {
            List<Sys_DQLog> list = new List<Sys_DQLog>();
            DataTable dt = new DataTable();
            HIDB.dbo con = base.conn(); 
            dt = con.GetDataTable(sql, sqlparams);

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {

                    Sys_DQLog model = new Sys_DQLog()
                    {
                        Guid = row["GUID"].ToString(),
                        ParentGUID = row["ParentGUID"].ToString(),
                        DQID = row["DQID"].ToString(),
                        DQName = row["DQName"].ToString(),
                        DQMessage = row["DQMessage"].ToString(),
                        DQTypeID = row["DQTypeID"].ToString(),
                        DQTime = Convert.ToDateTime(row["DQTime"]).ToString("yyyy-MM-dd HH:mm:ss")
                    };
                    list.Add(model);
                }
            }

            return list;
        }
        public int GetDQListCount(string sql, IDataParameter[] sqlparams)
        {
            DataTable dt = new DataTable();
            HIDB.dbo con = base.conn();
            dt = con.GetDataTable(sql, sqlparams);

            if (dt != null && dt.Rows.Count > 0)
            {
                return Int32.Parse(dt.Rows[0][0].ToString());
            }
            else
                return 0;
        }
        public bool ClearDQLog(string opUser, string date)
        {
            try
            {
                HIDB.dbo con = base.conn();
                con.SqlTimeOut = 120;
                IDataParameter[] sqlparams = new SqlParameter[1];
                sqlparams[0] = new SqlParameter("@DeletedDate", date);
                con.StoredUpdate("Working_db.UP_Sys_DQLog_ClearLog", sqlparams);
                con.SqlTimeOut = 60;
                return true;
            }
            catch (Exception ex) { throw ex; }
        }

        public bool ClearETLLog(string date)
        {
            try
            {
                HIDB.dbo con = base.conn();
                IDataParameter[] sqlparams = new SqlParameter[1];
                sqlparams[0] = new SqlParameter("@DeletedDate", date);
                con.StoredUpdate("Working_db.UP_Sys_ETLLog_ClearLog", sqlparams);
                return true;
            }
            catch (Exception ex) { throw ex; }
        }
    }
}